<?php
namespace API\Controller;
use Think\Controller;
class AddressController extends CommonController {
	//选择省份载入对应城市信息 
	// true表示载入成功 false失败
	//标识码 0表示失败 1表示成功
	public function loadcity(){
		$City = M('City');
		$provinceid = I("post.provinceid");
		$Citydata = $City->order("id ASC")->where("provinceid = {$provinceid}")->select(); 
		if (empty($Citydata)) {
		             $this->response("载入城市信息",false,0,"城市信息为空");	
		}else{
			$this->response("载入城市信息",true,1,$Citydata);
		}
	}
	//选择城市载入对应县区信息
	// true表示载入成功 false失败
	//标识码 0表示失败 1表示成功
	public function loaddistrict(){
		$District = M('District');
		$cityid = I("post.cityid");
		$Districtdata = $District->order("id ASC")->where("cityid = {$cityid}")->select();
		if (empty($Districtdata)) {
			$this->response("载入县区信息",false,0,"县区信息为空");	
		}else{
			$this->response("载入县区信息",true,1,$Districtdata);
		}
	}

	//改变默认地址
	public function changedefault(){
		$addressid = I("post.addressid");
		$userid = $_SESSION['USERID'];
		$Address = M('Useraddress');
		$map['id']  = array('not in',$addressid);
		$map['userid'] = array('eq',$userid);
		$res1=$Address->where($map)->setField('isdefault','0');
		$res2=$Address->where("id ={$addressid}")->setField("isdefault",'1');
		if ($res1 && $res2) {
			$this->response("修改默认地址",true,1,"成功修改默认地址");
		}else{
			$this->response("修改默认地址",false,0,"修改默认地址失败");	
		}
	}
	//删除地址
	public function deladdress(){
		$addressid = I("post.addressid");
		$Address = M('Useraddress');
		
		$res=$Address->where("id ={$addressid}")->delete();
		if (!$res) {
			$this->response("删除收货地址",false,0,"删除收货地址失败");	
		}else{
			$this->response("删除收货地址",true,1,"删除收货地址成功");
		}
		
	}
	
}